#include<bits/stdc++.h>
#define N 1000010
#define int long long
#define il inline
#define ct const
#define dl double
#define pk push_back
#define fi first
#define se second
#define sit set<pii>::iterator
#define pii pair<int,int>
using namespace std;
il int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
	}
	return x*f;
}
char f__[40];
il void write(int x){
	int cnt=0;
	if(x<0){
		putchar('-');x=-x;
	}
	if(!x) putchar('0');
	while(x){
		f__[cnt++]=x%10+'0';x/=10;
	}
	while(cnt) putchar(f__[--cnt]);
}
int n,son[N][2],ch[N][2],f[N],ans[N];
il void build(int x){
    if(!x) return;
    ch[x][0]=son[x][0];ch[x][1]=son[x][1];f[ch[x][0]]=f[ch[x][1]]=x;
    build(ch[x][0]);build(ch[x][1]);
}
il bool check(int fa,int x){
    return ch[fa][1]==x;
}
il void rotate(int x){
    int fa=f[x],ff=f[fa],k=check(fa,x),k_=check(ff,fa),tmp=ch[x][k^1];
    f[fa]=x;ch[x][k^1]=fa;
    f[tmp]=fa;ch[fa][k]=tmp;
    f[x]=ff;ch[ff][k_]=x;
    f[0]=ch[0][0]=ch[0][1]=0;
}
il void splay(int x){
    // cerr<<x<<" ";
    while(f[x]){
        // cerr<<"ERROR";
        int fa=f[x],ff=f[f[x]];
        if(!ff) rotate(x);
        else if(check(ff,fa)==check(fa,x)){
            rotate(fa);rotate(x);
        }
        else{
            rotate(x);rotate(x);
        }
    }
}
il void dfs(int x,int dep){
    if(!x) return;
    ans[x]+=dep;++dep;
    // cerr<<x<<" "<<ch[x][0]<<" "<<ch[x][1]<<"\n";
    dfs(ch[x][0],dep);dfs(ch[x][1],dep);
}
signed main(){
    freopen("dream.in","r",stdin);
    freopen("dream.out","w",stdout);
    n=read();int flag=1;
    for(int i=1;i<=n;++i){
        son[i][0]=read();son[i][1]=read();
        if(!son[i][1]) flag=0;
    }
    if(flag){
        int x=1,dep=0;
        while(x){
            ans[x]=dep*(n-1)+(n-dep-1)/2;
            ++dep;
            x=son[x][0];
        }
    }
    else{
        for(int i=1;i<=n;++i){
            f[1]=0;build(1);splay(i);dfs(i,0);
        }
    }
    for(int i=1;i<=n;++i){
        write(ans[i]);putchar('\n');
    }
	return 0;
}